package com.jemmyee.pizza.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.Role;
import com.jemmyee.pizza.service.IRoleService;
import com.jemmyee.pizza.web.Constants;


/**
 * @Description:角色管理
 * @author:jemmyee@gmail.com
 * @date:2010-10-28
 * @version:v1.0
 */
@Controller
@RequestMapping("/admin/role")
public class RoleController {
	
	private static Log log= LogFactory.getLog(RoleController.class);
	
	@Autowired
	private IRoleService<Role> roleService;
	

	@RequestMapping("/to_add")
	public ModelAndView toAdd(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("/admin/role/add");
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id,HttpServletRequest request){
		   ModelAndView mv=new ModelAndView();
		   mv.setViewName("/admin/role/edit");
		   mv.addObject("role", roleService.findById(id));
		   return mv;
	}
	
	@RequestMapping("/to_add_right/{id}")
	public ModelAndView toAddRight(@PathVariable Integer id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		request.getSession().setAttribute("online_rid",id);
		mv.setViewName("/admin/role/add_right");
		return mv;
	}
	
	@RequestMapping("/to_edit_right/{id}")
	public ModelAndView toEditRight(@PathVariable Integer id,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		request.getSession().setAttribute("online_rid",id);
		mv.addObject("item",roleService.findById(id));
		mv.setViewName("/admin/role/add_right");
		return mv;
	}
	
	@RequestMapping("/add_right/{ids}")
	@ResponseBody
	public Map<String, Object> addRight(@PathVariable String ids,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		Role role=roleService.findById(Integer.parseInt(request.getSession().getAttribute("online_rid").toString()));
		role.setRights(ids);
		roleService.update(role);
		model.put("msg","succ");
		return model;
	}
	
	
	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(@RequestBody Role role,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		Role temp=null;
		if(roleService.findByProperty("name",role.getName())!=null&&roleService.findByProperty("name",role.getName()).size()>0){
			temp=roleService.findByProperty("name",role.getName()).get(0);
		}
		if(temp!=null&&temp.getName().equals(role.getName().trim()))
		{
			model.put("msg","error");
		}else
		{
			role.setRights("0");
			role.setSort(0);
			role.setStatus(Short.valueOf("0"));
			roleService.save(role);
			model.put("msg","succ");
		}
		return model;
	}


	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody Role role,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		Role temp=null;
		if(roleService.findByProperty("name",role.getName())!=null&&roleService.findByProperty("name",role.getName()).size()>0){
			temp=roleService.findByProperty("name",role.getName()).get(0);
		}
		Role real=roleService.findById(role.getId());
		if(temp!=null&&temp.getName().equals(role.getName().trim())&&!temp.getName().equals(real.getName()))
		{
			model.put("msg","error");
		}else
		{
			real.setName(role.getName());
			real.setRemark(role.getRemark());
			roleService.update(real);
			model.put("msg","succ");
		}
		return model;
	}

	
	@RequestMapping(value="/list/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,HttpServletRequest request){
		   DetachedCriteria dc= DetachedCriteria.forClass(Role.class);
		   ModelAndView mv=new ModelAndView();
		   Page page=roleService.findPageByCriteria(dc,1000, startIndex,null);
		   mv.addObject("page",page);
		   mv.setViewName("/admin/role/list");
		   return mv;
	}
	
	@RequestMapping(value="/delete/{id}")
	public String delete(@PathVariable Integer id,HttpServletRequest request){
	    roleService.delete(roleService.findById(id));
	    return "redirect:/admin/role/list/0";
	}

}
